package ephrecorder

import (
	"gitee.com/ymofen/golog"
	"gitee.com/ymofen/sputils"
)

type Wrapper struct {
	Id       string
	log      golog.Logger
	SaveFmt  string
	Interval int64
	er       *EphRecorder
	obsSub   *sputils.SpBaseSub
	ephSub   *sputils.SpBaseSub
}

func (this *Wrapper) Init() {
	this.obsSub = sputils.NewSpBaseSub("")
	this.ephSub = sputils.NewSpBaseSub("")
	this.er = NewEphRecorder(this.SaveFmt)
	this.er.interval = this.Interval
	this.log = golog.Default().WithOptionEx(golog.WithOptionPrefix(this.Id))
}

func (this *Wrapper) Close() error {
	this.obsSub.Close()
	this.ephSub.Close()
	this.er.Close()
	return nil
}

func (this *Wrapper) onObsInput(id, topic string, args ...interface{}) (ok bool) {
	action := args[0].(int)
	if action == sputils.SpTypeData {
		buf := args[1].([]byte)
		this.er.InputObsBuf(buf)
	}
	return
}

func (this *Wrapper) onEphInput(id, topic string, args ...interface{}) (ok bool) {
	action := args[0].(int)
	if action == sputils.SpTypeData {
		buf := args[1].([]byte)
		this.er.InputEphBuf(buf)
	}
	return
}

func (this *Wrapper) UpdateConf(obsSubConf string, ephSubConf string) {
	this.obsSub.ReloadConfString(obsSubConf, this.onObsInput)
	this.ephSub.ReloadConfString(ephSubConf, this.onEphInput)
}
